package stack.每日温度;

import java.util.LinkedList;

public class Solution {
    public int[] dailyTemperatures(int[] temperatures) {
        int[] ans = new int[temperatures.length];
        LinkedList<Integer> list = new LinkedList<>();
        //当栈顶元素小于即将入栈的元素时，栈顶元素出栈
        list.addLast(0);
        for (int i = 1; i < temperatures.length; i++) {
            while (!list.isEmpty() &&  temperatures[list.peekLast()] < temperatures[i]){
                Integer index = list.removeLast();
                ans[index] =  i - index;
            }
            list.addLast(i);
        }
        return ans;
    }

    public static void main(String[] args) {
        Solution solution = new Solution();
        int[] ints = solution.dailyTemperatures(new int[]{73, 74, 75, 71, 69, 72, 76, 73});
        for (int anInt : ints) {
            System.out.print(anInt + " ");
        }
        System.out.println();
    }
    /**
     * 72 69 71
     * 1,1,4,2,1,1,0,0
     * 1 1 4 3 2 1 0 0
     */
}
